package cn.alm.springmvcrestfulcrud.controller;

import cn.alm.springmvcrestfulcrud.common.CR;
import cn.alm.springmvcrestfulcrud.entity.Employee;
import cn.alm.springmvcrestfulcrud.service.EmployeeService;
import cn.alm.springmvcrestfulcrud.vo.EmployeeAddVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("employee")
@Slf4j
@Tag(name = "员工管理")
public class EmployeeController {

    @Resource
    EmployeeService employeeService;

    @PostMapping("list")
    @Operation(summary = "员工列表")
    public CR list() {
        log.info("employee controller, list");
        List<Employee> list = employeeService.list();
        return CR.success("success", list);
    }

    @PostMapping("getById")
    @Operation(summary = "根据id获取员工信息")
    public CR getById(@RequestBody Employee employee) {
        log.info("employee controller, getById");
        Employee getById = employeeService.getById(employee.getId());
        return CR.success("success", getById);
    }

    @PostMapping("add")
    @Operation(summary = "新增员工")
    public CR add(@RequestBody @Valid EmployeeAddVo employeeAddVo) {
        log.info("employee controller, add");
        // 将vo转换成entity
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeAddVo, employee);
        Integer add = employeeService.add(employee);
        if (add <= 0) {
            throw new RuntimeException("add fail");
        }
        return CR.success("添加成功", null);
    }

//    @PostMapping("add")
//    public CR add(@RequestBody @Valid Employee employee, BindingResult bindingResult) {
//        log.info("employee controller, add");
//        // Valid发现有错误产生
//        if (bindingResult.hasErrors()) {
//            log.error("employee controller, add error");
//            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
//            HashMap<String, String> errMap = new HashMap<>();
//            for (FieldError fieldError : fieldErrors) {
//                String field = fieldError.getField();
//                String message = fieldError.getDefaultMessage();
//                errMap.put(field, message);
//            }
//            return CR.create(500, "操作失败", errMap);
//        }
//        Integer add = employeeService.add(employee);
//        if (add <= 0) {
//            throw new RuntimeException("add fail");
//        }
//        return CR.success("添加成功", null);
//    }
}
